
package com.tianpengtech.common;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.config.Routes.Route;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.tianpengtech.admin.AdminRoutes;
import com.tianpengtech.common.generator.app.Generator;
import com.tianpengtech.common.handler.H5AdHandler;
import com.tianpengtech.common.handler.WebSocketHandler;
import com.tianpengtech.common.intercepter.XSSInterceptor;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.appsetting.AppSetting;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.dictionary.Dictionary;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairadvertisement.FairAdvertisement;
import com.tianpengtech.common.model.fairauth.FairAuth;
import com.tianpengtech.common.model.fairbankno.FairBankNo;
import com.tianpengtech.common.model.fairext.FairExt;
import com.tianpengtech.common.model.fairmessage.FairMessage;
import com.tianpengtech.common.model.fairorder.FairOrder;
import com.tianpengtech.common.model.fairphoto.FairPhoto;
import com.tianpengtech.common.model.fairreport.FairReport;
import com.tianpengtech.common.model.fairsalary.FairSalary;
import com.tianpengtech.common.model.fairwithdraw.FairWithdraw;
import com.tianpengtech.common.model.feedback.FeedBack;
import com.tianpengtech.common.model.message.Message;
import com.tianpengtech.common.model.moneylog.MoneyLog;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.model.permission.Permission;
import com.tianpengtech.common.model.permission.PermissionDispather;
import com.tianpengtech.common.model.platformincome.PlatFormIncome;
import com.tianpengtech.common.model.role.Role;
import com.tianpengtech.common.model.task.*;
import com.tianpengtech.common.model.taskscreenshot.TaskScreenShot;
import com.tianpengtech.common.model.user.Session;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.model.websitead.WebSiteAd;
import com.tianpengtech.common.model.websiteemployee.WebSiteEmployee;
import com.tianpengtech.common.model.websitemsg.WebSiteMsg;
import com.tianpengtech.common.model.websitewh.WebSiteWh;
import com.tianpengtech.common.plugin.im.ImStartPlugin;
import com.tianpengtech.common.plugin.quartz.QuartzPlugin;
import com.tianpengtech.common.plugin.shiro.ext.ShiroPlugin;
import com.tianpengtech.common.plugin.shiro.tag.ShiroTag;
import com.tianpengtech.common.util.DruidUtil;
import com.tianpengtech.common.util.PageUtil;
import com.tianpengtech.common.util.QiNiuUtil;
import com.tianpengtech.front.mobile.MobileRoutes;
import com.tianpengtech.front.web.WebRoutes;
import com.tianpengtech.front.web.common.util.WebPageUtil;
import com.tianpengtech.front.web.job.SessionJob;
import com.tianpengtech.front.web.job.TaskEndJob;
import com.tianpengtech.front.web.job.TaskStartJob;
import com.tianpengtech.front.web.tasktemplate.TaskTemplateController;
import com.tianpengtech.front.website.H5AdController;
import com.tianpengtech.front.website.WebSiteRoutes;

import java.lang.reflect.Method;
import java.util.Properties;

public class MyConfig extends JFinalConfig {

	private Routes routes;
    public Properties loadProp(String pro, String dev) {
        try {
            return loadPropertyFile(pro);
        } catch (Exception e) {
            return loadPropertyFile(dev);
        }
    }

    @Override
    public void configConstant(Constants me) {
        PropKit.use("a_little_config.txt");
        me.setDevMode(PropKit.getBoolean("devMode", false));
//        me.setBaseUploadPath(PropKit.get("user.upload.dir"));
    }

    @Override
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("contextPath"));// 设置上下文路路径 防止样式丢失
        me.add(new WebSocketHandler("^/websocket"));
        me.add(DruidUtil.getDruidStatViewHandler()); // druid 统计页面功能
       // me.add(new H5AdHandler());
    }

    @Override
    public void configInterceptor(Interceptors me) {

        //XSS拦截器
        me.add(new XSSInterceptor());
    }

    @Override
    public void configPlugin(Plugins me) {
        DruidPlugin druidPlugin = druidPluginConfig(me);
        ActiveRecordConfig(me, druidPlugin);
        shiroConfig(me);
        //配置Quartz插件
//        me.add(new QuartzPlugin());
        
        me.add(new ImStartPlugin());
        me.add(new QuartzPlugin());
    }

    /*
        配置DruidPlugin
     */
    private DruidPlugin druidPluginConfig(Plugins me) {
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
                PropKit.get("user"), PropKit.get("password").trim());
        WallFilter wallFilter = new WallFilter();              // 加强数据库安全
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        druidPlugin.addFilter(new StatFilter());
        me.add(druidPlugin);
        return druidPlugin;
    }

    /*
        配置Shiro
     */
    private void shiroConfig(Plugins me) {
        ShiroPlugin shiroPlugin = new ShiroPlugin(new AdminRoutes());
        shiroPlugin.setLoginUrl("/admin/login");
        shiroPlugin.setSuccessUrl("/admin/");
        shiroPlugin.setUnauthorizedUrl("/admin/forbidden");
        me.add(shiroPlugin);
    }

    /*

     */
    private void ActiveRecordConfig(Plugins me, DruidPlugin druidPlugin) {
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(true);
        me.add(arp);
        arp.addMapping(Account.dao.tableName(), Account.class);
        arp.addMapping(Role.dao.tableName(),Role.class);
        arp.addMapping(Dictionary.dao.tableName(),Dictionary.class);
        arp.addMapping(Permission.dao.tableName(),Permission.class);
        arp.addMapping(PermissionDispather.dao.tableName(),PermissionDispather.class);
        arp.addMapping(Authenticate.dao.tableName(),Authenticate.class);
        arp.addMapping(Task.dao.tableName(),Task.class);
        arp.addMapping(TaskWh.dao.tableName(),TaskWh.class);
        arp.addMapping(TaskCount.dao.tableName(),TaskCount.class);
        arp.addMapping(TaskCVS.dao.tableName(),TaskCVS.class);
        arp.addMapping(TaskSource.dao.tableName(),TaskSource.class);
        arp.addMapping(Fair.dao.tableName(),Fair.class);
        arp.addMapping(MoneyLog.dao.tableName(),MoneyLog.class);
        arp.addMapping(Message.dao.tableName(),Message.class);
        arp.addMapping(FairMessage.dao.tableName(),FairMessage.class);
        arp.addMapping(User.dao.tableName(),User.class);
        arp.addMapping(Opertion.dao.tableName(),Opertion.class);
        arp.addMapping(Session.dao.tableName(),Session.class);
        arp.addMapping(FeedBack.dao.tableName(),FeedBack.class);
        arp.addMapping(FairExt.dao.tableName(),FairExt.class);
        arp.addMapping(FairBankNo.dao.tableName(),FairBankNo.class);
        arp.addMapping(FairAuth.dao.tableName(),FairAuth.class);
        arp.addMapping(FairWithdraw.dao.tableName(),FairWithdraw.class);
        arp.addMapping(FairReport.dao.tableName(),FairReport.class);
        arp.addMapping(FairAdvertisement.dao.tableName(),FairAdvertisement.class);
        arp.addMapping(FairPhoto.dao.tableName(),FairPhoto.class);
        arp.addMapping(WebSiteWh.dao.tableName(),WebSiteWh.class);
        arp.addMapping(WebSiteEmployee.dao.tableName(),WebSiteEmployee.class);
        arp.addMapping(WebSiteMsg.dao.tableName(),WebSiteMsg.class);
        arp.addMapping(TaskScreenShot.dao.tableName(),TaskScreenShot.class);
        arp.addMapping(AppSetting.dao.tableName(),AppSetting.class);
        arp.addMapping(FairOrder.dao.tableName(),FairOrder.class);
        arp.addMapping(FairSalary.dao.tableName(),FairSalary.class);
        arp.addMapping(WebSiteAd.dao.tableName(),WebSiteAd.class);
        arp.addMapping(PlatFormIncome.dao.tableName(),PlatFormIncome.class);
        EhCachePlugin cachePlugin = new EhCachePlugin();
        me.add(cachePlugin);
    }

    @Override
    public void configRoute(Routes me) {

        me.add(new AdminRoutes());
        me.add(new WebRoutes());
        me.add(new WebSiteRoutes());
        me.add(new MobileRoutes());
        
        me.add("/tasktemp", TaskTemplateController.class,"/task_template");
        me.add("/h5",H5AdController.class,"/h5");
    }

    public void afterJFinalStart() {
        //添加定时任务
        //0 5 0 * * ?
        QuartzPlugin.addJob("TaskJob_first", "0 5 0 * * ?", TaskStartJob.class);//每天凌晨12：05检查当天需要被标记进行中的任务
        QuartzPlugin.addJob("TaskJob_second", "0 50 23 * * ?", TaskEndJob.class);//每天11:50检查当天需要结束的任务
        QuartzPlugin.addJob("SessionJob", "0 0/2 * * * ?", SessionJob.class);
        
        
//        for (Route r : this.routes.getRouteItemList()) {
//    		System.out.println(r.getControllerClass()+":"+r.getControllerKey());
//		}
        
    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 8091,"/",5);
    }

    @Override
    public void configEngine(Engine me) {
    	me.setDevMode(true);
    	me.addSharedFunction("/WEB-INF/view/admin/macro/macro.html");
        me.addSharedFunction("/WEB-INF/view/admin/lay_out/__layout.html");
        
        me.addSharedFunction("/WEB-INF/view/web_site/common/__layout.html");
        me.addSharedFunction("/task_template/common/__task_base_template.html");
        
        me.addSharedStaticMethod(Generator.class);
        me.addSharedStaticMethod(ShiroTag.class);
        me.addSharedStaticMethod(PageUtil.class);
        me.addSharedObject("AppName","颜控网络运营支持系统平台");

        /* web */
        WebPageUtil webPageUtil = new WebPageUtil(me);
        webPageUtil.loadPropToEngine();
        me.addSharedFunction(webPageUtil.LAYOUT_PATH);
        me.addSharedObject(webPageUtil.UTILNAME,webPageUtil);
        me.addSharedStaticMethod(WebPageUtil.class);
        me.addSharedObject("domain",QiNiuUtil.DOMAIN);
    }
}
